##########
#Playing to the Gallery: Emotive Rhetoric in Parliaments
#Moritz Osnabruegge, Sara B. Hobolt, Toni Rodon
#This script produces Figure 3
#########


rm(list=ls())

library("gdata")
library("foreign")
library("tidyverse")
library("ggplot2")

#Specify the path here
setwd("")

#Run "1-uk.do" (lines 52-76) to generate the txt-files
data1 = read.csv("pm_questions.txt", sep="\t")
data1$type="Prime Minister's Questions"
data2 = read.csv("queen_debate_day1.txt", sep="\t")
data2$type="Queen's Speech Debate: Opening Day"
data3 = read.csv("queen_debate_others.txt", sep="\t")
data3$type="Queen's Speech Debate: Other Days"
data4 = read.csv("m_questions.txt", sep="\t")
data4$type = "Ministerial Question Time"
data5 = read.csv("u_questions.txt", sep="\t")
data5$type = "Urgent Question"
data6 = read.csv("others.txt", sep="\t")
data6$type= "Others"

data = rbind(data1, data2, data3, data4, data5, data6)
data = rename.vars(data, from="X", to="var")
data = rename.vars(data, from="X_cons", to="pe")

data$X.1 = NULL

#Subset relevant data
data = subset(data, var=="ul" | var=="ll" | var=="b")

data = spread(data, var, pe)

data$type = as.factor(data$type)
data$type = factor(data$type,levels=c("Others",  "Urgent Question", "Ministerial Question Time", "Queen's Speech Debate: Other Days","Queen's Speech Debate: Opening Day", "Prime Minister's Questions"), ordered=TRUE)


#Figure 3
p = ggplot(data=data) + geom_pointrange(mapping=aes(y=b, x=type, ymin=ll, ymax=ul)) +
    coord_flip(ylim=c(0,3.5)) + 
    scale_y_continuous(name="Predicted level of emotive rhetoric", breaks=seq(0,4, by=0.5)) +
    scale_x_discrete(name="") + 
    theme_bw() + theme(axis.line=element_line(color="black"),
    axis.text.x=element_text(size=12, colour="black"),
    axis.text.y=element_text(size=12, colour="black", hjust=0), 
    axis.title.y=element_text(size=12),
    axis.title.x=element_text(size=12))

pdf("figure_3.pdf", width=7, height=3.5) 
print(p)
dev.off()